<?php
class MaintenanceAllotModel extends BaseModel
{
    /**
     * 根据用户获取任务列表
     * @param $uid
     * @param $page
     * @param string $fields
     * @return array
     */
    public function getUserWaitTasks($uid, $limit, $fields = 'm_maintenance.*, m_maintenance_allot.itemid as aid')
    {
        $map = array();
        $map['uid'] = $uid;
        $map['status'] = array(array('neq', 100), array('neq', 0), array('neq', 99), 'and');
        return $this->field($fields)
            ->where($map)
            ->join('INNER JOIN m_maintenance ON m_maintenance.itemid = m_maintenance_allot.mid')
            ->limit($limit)
            ->order('m_maintenance.itemid DESC')
            ->select();
    }

    /**
     * 获取相对人待处理任务数量
     * @param $uid
     * @return mixed
     * @author ice<iceinto@mallog.com.cn>
     */
    public function getUserWaitCount($uid)
    {
        $map = array();
        $map['uid'] = $uid;
        $map['status'] = array(array('neq', 100), array('neq', 0), array('neq', 99), 'and');
        return $this->where($map)
            ->join('INNER JOIN m_maintenance ON m_maintenance.itemid = m_maintenance_allot.mid')
            ->count();
    }

    /**
     * 获取分配任务
     * @param $map
     * @param $limit
     * @param string $fields
     * @return mixed
     * @author ice<iceinto@mallog.com.cn>
     */
    public function getTasks($map, $limit, $fields = 'm_maintenance.*, m_maintenance_allot.itemid as aid')
    {
        return $this->field($fields)
            ->where($map)
            ->join('INNER JOIN m_maintenance ON m_maintenance.itemid = m_maintenance_allot.mid')
            ->limit($limit)
            ->order('m_maintenance.itemid DESC')
            ->select();
    }

    /**
     * 获取分配任务数量
     * @param $map
     * @return mixed
     * @author ice<iceinto@mallog.com.cn>
     */
    public function getTaskCount($map)
    {
        return $this->where($map)
            ->join('INNER JOIN m_maintenance ON m_maintenance.itemid = m_maintenance_allot.mid')
            ->count();
    }

    /**
     * 获取任务相对图片数量
     * @param $map
     * @return mixed
     * @author ice<iceinto@mallog.com.cn>
     */
    public function getTaskImageCount($map)
    {
        return $this->field('sum(m_maintenance.end_count - m_maintenance.now_count) as img_count')
            ->where($map)
            ->join('INNER JOIN m_maintenance ON m_maintenance.itemid = m_maintenance_allot.mid')
            ->find();
    }

    /**
     * 获取相对人待处理任务数量
     * @param $uid
     * @return mixed
     * @author ice<iceinto@mallog.com.cn>
     */
    public function getUserLastAllot($uid)
    {
        $map = array();
        $map['uid'] = $uid;
        return $this->where($map)->order('itemid DESC')->find();
    }
}